

#import "BasicRenderer.h"
#import "GameController.h"


@implementation BasicRenderer (Private)
- (void)initOpenGL {
	
    NSLog(@"INFO - ES1Renderer: Initializing OpenGL");
    
	// Switch to GL_PROJECTION matrix mode and reset the current matrix with the identity matrix
	glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    
	glOrthof(0, contextWidth, 0, contextHeight, -1, 1);
	
    // Set the viewport
    glViewport(0, 0, contextWidth , contextHeight);
    
    NSLog(@"INFO - ES1Renderer: Setting glOrthof to width=%d and height=%d", contextWidth, contextHeight);
    
	// Switch to GL_MODELVIEW so we can now draw our objects
	glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    
	// Set the colour to use when clearing the screen with glClear
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    
	// We are not using the depth buffer in our 2D game so depth testing can be disabled.  If depth
	// testing was required then a depth buffer would need to be created as well as enabling the depth
	// test
	glDisable(GL_DEPTH_TEST);
    
    // Enable the OpenGL states we are going to be using when rendering
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
	
	
}
@end



@implementation BasicRenderer

- (id) init
{
	if (self = [super init])
	{
		context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1];
        
        if (!context || ![EAGLContext setCurrentContext:context])
		{
            [self release];
            return nil;
        }
		
		glGenFramebuffersOES(1, &framebuffer);
		glGenRenderbuffersOES(1, &renderbuffer);
		glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);
		glBindRenderbufferOES(GL_RENDERBUFFER_OES, renderbuffer);
		glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, renderbuffer);
		sharedGameController = [GameController sharedGameController];
	}
	
	return self;
}

- (void) render {
    
	 glClear(GL_COLOR_BUFFER_BIT);
	
	[sharedGameController renderScene];
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];
	
   /* static const GLfloat squareVertices[] = {
        -0.5f,  -0.33f,
		0.5f,  -0.33f,
        -0.5f,   0.33f,
		0.5f,   0.33f,
    };
	
    static const GLubyte squareColors[] = {
        255, 255,   0, 255,
        0,   255, 255, 255,
        0,     0,   0,   0,
        255,   0, 255, 255,
    };
    
		static float transY = 0.0f;
	
    [EAGLContext setCurrentContext:context];
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, framebuffer);
    glViewport(0, 0, contextWidth, contextHeight);
    
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	    glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);
		transY += 0.075f;

   
    
    glVertexPointer(2, GL_FLOAT, 0, squareVertices);
    glEnableClientState(GL_VERTEX_ARRAY);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
    glEnableClientState(GL_COLOR_ARRAY);
    
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, renderbuffer);
	
    [context presentRenderbuffer:GL_RENDERBUFFER_OES];*/
}

- (BOOL) resizeFromLayer:(CAEAGLLayer *)layer
{	
	// Allocate color buffer backing based on the current layer size
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, renderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER_OES fromDrawable:layer];
	glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &contextWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &contextHeight);
	
    if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES)
	{
		NSLog(@"Failed to make complete framebuffer object %x", glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES));
        return NO;
    }
    
	[self initOpenGL];
    return YES;
}

- (void) dealloc
{
	// Tear down GL
	if (framebuffer)
	{
		glDeleteFramebuffersOES(1, &framebuffer);
		framebuffer = 0;
	}
	
	if (renderbuffer)
	{
		glDeleteRenderbuffersOES(1, &renderbuffer);
		renderbuffer = 0;
	}
	
	// Tear down context
	if ([EAGLContext currentContext] == context)
        [EAGLContext setCurrentContext:nil];
	
	[context release];
	context = nil;
	
	[super dealloc];
}

@end


